package com.pactera.madp.common.crypto.security;

import com.pactera.madp.common.crypto.constant.enums.CryptoEnum;
import com.pactera.madp.common.crypto.sm.SM2;
import com.pactera.madp.common.crypto.sm.SM2Impl;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigInteger;
import java.security.KeyPair;

/**
 * @author Deming.Chang
 * @date 2019-12-04
 * @title 自定义非对称密钥存储
 */
@Data
@NoArgsConstructor
public class MadpKeyPair implements Serializable {
	private static final long serialVersionUID = 9033121609438490585L;

	/**
	 * 国标密钥
	 * @param keyPair 国标密钥
	 * @param cryptoType 加密算法标准
	 */
	public MadpKeyPair(KeyPair keyPair, CryptoEnum cryptoType) {
		super();
		this.privateKey = keyPair.getPrivate().getEncoded();
		this.publicKey = keyPair.getPublic().getEncoded();
		this.cryptoType = cryptoType;
	}

	/**
	 * 国密密钥
	 * @param keyPair 国密密钥
	 * @param cryptoType 加密算法标准
	 */
	public MadpKeyPair(SM2Impl.SM2KeyPair keyPair, CryptoEnum cryptoType) {
		super();
		BigInteger privateKeyInteger = keyPair.getPrivateKey();
		this.privateKey = SM2.bigInteger2Bytes(privateKeyInteger);
		this.publicKey =  keyPair.getPublicKey().getEncoded();
		this.cryptoType = cryptoType;
	}

	/**
	 * 二进制私钥
	 */
	private byte[] privateKey;

	/**
	 * 二进制公钥
	 */
	private byte[] publicKey;

	/**
	 * 加密算法标准
	 */
	private CryptoEnum cryptoType;

}
